// pages/jielong/myorder/myorder.js
var app = getApp(); //引用app.js
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var baseURL1 = app.data.baseURL;
var jielong = app.data.jielong;
var jlpay1 = app.data.jlway;
const util = require("../../../util/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nav: ['全部订单','待发货', '待收货', '已完成'],
    selectIndex: 0,
    statu:'',
    zhuangtai:[],
    text:'暂无订单'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    let type = ''
    if (options.type) {
      type = options.type
    } else {
      type = ''
    }
    let index = ''
    switch(type){
      case '2':
        index=2
        break;
      case '8':
       index=1
       break;
      case '3':
       index=3
       break;
    }
    this.setData({
      statu: type,
      selectIndex:index
    },()=>{
      this.getdata();
     
    })
    
  },
  getdata: function () {
    var that = this;
    wx.request({
      url: jlpay1 + 'op=getMyselfOrders',
      data: {
        openid:wx.getStorageSync('openid'),
        unionId: app.data.unionId || wx.getStorageSync('unionId'),
        statu: that.data.statu
      },
      success: function (res) {
        wx.hideLoading()
        console.log(res)
        if (res.data.data==0){
          if (that.data.statu==0){
            that.setData({
              text:'您还没有订单'
            })
          }
          if (that.data.statu == 1) {
            that.setData({
              text: '您还没有待发货订单'
            })
          }
          if (that.data.statu == 2) {
            that.setData({
              text: '您还没有待收货订单'
            })
          }
          if (that.data.statu == 3) {
            that.setData({
              text: '您还没有已完成订单'
            })
          }
        }
        // 0待付款 1已付款 2待收货 8 待发货 3已签收 4已退款 7已退款(现在在用) 5没用上 6部分退款 10待退款 9 已取消 11 退款异常
        // for(var i=0;i<res.data.data.length;i++){
        //   if(res.data.data[i].status.status==0){

        //   }
        // }
        that.setData({
          list: res.data.data
        },()=>{
          let zhuangtai = []
          for(let i=0;i<res.data.data.length;i++){
            zhuangtai.push(that.zhuangtai(res.data.data[i].status))
          }
          that.setData({
            zhuangtai: zhuangtai
          })
        })
      }
    })
  },
  selectNav: function (e) {  
   // 0待付款 1已付款 2待收货 8 待发货 3已签收 4已退款 7已退款(现在在用) 5没用上 6部分退款 10待退款 9 已取消 11 退款异常
    this.setData({
      selectIndex: e.currentTarget.dataset.index
    })
    let statu = ''
    if (e.currentTarget.dataset.index == 0) {
      this.setData({
        statu: '',
        hexiao: ''
      })
    } else if (e.currentTarget.dataset.index == 1) {
      this.setData({
        statu: 8,
        hexiao: 1
      })
      statu = ''
    } else if (e.currentTarget.dataset.index == 2) {
      this.setData({
        statu: 2,
        hexiao: 1
      })
      statu = 2
    } else {
      this.setData({
        statu: 3,
        hexiao: ''
      })
      statu = 3
    }
    this.getdata();
  },
  goorderdetail: function (e) {
    console.log(e.currentTarget)
    let that = this
    wx.navigateTo({
      url: '../orderdetail/orderdetail?orderno=' + e.currentTarget.dataset.index,
    })
  },
  verify: function (e) {
    console.log(e)
    var orderno = e.currentTarget.dataset.orderno;
    var areaid = e.currentTarget.dataset.areaid;
    console.log(orderno)
    wx.scanCode({
      onlyFromCamera: true,
      success(res) {
        console.log(res)
        wx.request({
          url: commonURL + 'op=isScan',
          data: {
            openid: app.data.openid,
            orderno: orderno,
            unionId: app.data.unionId || wx.getStorageSync('unionId'),
            uniacid: app.data.GZHid,
            tuan_openid: util.parseURL(res.result).mid
          },
          success: function (res) {
            if (res.data.status == 1) {
              wx.navigateTo({
                url: '../check/check?orderno=' + orderno + '&areaid=' + areaid,
              })
            } else {
              wx.showToast({
                title: '暂无核销权限',
              })
            }
          }
        })
      }
    })
  },
  zhuangtai: function (status) {
    switch (status) {
      case "0": //等待 付款******  
        return "待付款";
        break;
      case "1": //支付成功  
        return "已支付";
        break;
      case "8": //等待商家发货    0 1 8
        return "待发货";
        break;
      case "2": //订单已发货  0 1 8 2
        return "待收货";
        break;
      case "3": //订单完成  0 1 8 2 3
        return "已收货";
        break;
      case "10": //等待退款 0 1 10
        return "待退款";
        break;
      case "4": //订单已退款  0 1 10 4
        return "已退款";
        break;
      case "7": //订单已退款  0 1 10 7
        return "已退款";
        break;
      case "6": //部分退款  0 1 8 2 6
        return "部分退款";
        break;
      case "9": //订单已取消  0 9****
        return "已取消";
        break;
      case "5": //订单已取消  0 9****
        return "部分签收";
        break;
    }
  },
  copyTBL:function(e){
    console.log(e.currentTarget)
    wx.setClipboardData({
      data: e.currentTarget.dataset.index,
      success:function(res){
        
      }
    })
  },
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})